package controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jsr107cache.Cache;
import net.sf.jsr107cache.CacheException;
import net.sf.jsr107cache.CacheManager;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.google.appengine.api.memcache.jsr107cache.GCacheFactory;

@SuppressWarnings("serial")
public class ExpDomainServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		String tld = req.getParameter("tld");
		String length = req.getParameter("length");
		String content = "";
		String strs = "";
		if(tld!=null&&length!=null)
		{
			String name = tld+length;
			
			Cache cache=null;
			Map props = new HashMap();
	        props.put(GCacheFactory.EXPIRATION_DELTA, 300);
	
			try {
	            cache = CacheManager.getInstance().getCacheFactory().createCache(props);
	        } catch (CacheException e) {
	           e.printStackTrace();
	           
	        }
	        if(cache.containsKey(name))
	        {
	        	@SuppressWarnings("unchecked")
				String expiring = (String)cache.get(name);
	   
	        	strs =  expiring;        	
	        }
	        else
	        {
	        	try {
	    			URL url = new URL("http://107.21.108.48:8080/expiringdomain/newexpiring");
	    	        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
	    	        connection.setDoOutput(true);
	    	        connection.setReadTimeout(500000);
	    			connection.setConnectTimeout(1000000);
	    	        connection.setRequestMethod("POST");
	    	        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
	    	        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
	    	        writer.write("tld=" + tld);
	    	        writer.write("&length="+length);
	    	        writer.close();
	    	        
	    	        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
	    	        	
	    	        	 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
		    			
		    			while(1==1)
		    			{
		    				String str = reader.readLine();
		    				if(str==null) break;
		    				content+=str;
		    				
		    			}
	    	        }
	    			Document doc = Jsoup.parse(content);
	    			System.out.println(doc);
	    			strs = doc.select("body").html();
	    		} catch (Exception e) {
	    			e.printStackTrace();
	    		}
	        	cache.put(name, strs);
	        }
		}
        req.setAttribute("strs", strs);
        
		try {
			req.getRequestDispatcher("exp.jsp").forward(req, resp);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
